import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';
import { ManageCardAddComponent } from '../manage-card-add/manage-card-add.component';
import { ManageCardEditComponent } from '../manage-card-edit/manage-card-edit.component';

@Component({
  selector: 'app-manage-card-list',
  templateUrl: './manage-card-list.component.html',
  styles: []
})
export class ManageCardListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  frozenFlag:boolean = this.aclService.canAbility("door-card/frozen");//检查用户按钮权限
  setFlag:boolean =  this.aclService.canAbility("door-card/set");
  cancelFlag:boolean =  this.aclService.canAbility("door-card/cancel");
  delFlag:boolean =  this.aclService.canAbility("door-card/delete");
  addFlag:boolean =  this.aclService.canAbility("door-card/add");
  unfrozenFlag:boolean =  this.aclService.canAbility("door-card/unfrozen");
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '卡号',
        // fFn: (item, columu) => this.delCardZero(item.cardNo),
        // filter: {
        //   placeholder: '请输入卡号',
        //   type: 'string'
        // }
      },
      {
        index: 'cardNo',
        title: '虚拟卡号',
        //fFn: (item, columu) => this.delCardZero(item.cardNo),
        filter: {
          placeholder: '请输入虚拟卡号',
          type: 'string'
        }
      },
      {
        index: "cardCode",
        title:"卡编号",
        filter: {
          placeholder:'请输入卡编号',
          type:'string'
        }
      },
      {
        index: 'cardType',
        title: '门卡类型',
        type: ColumnType.tag,
        tag: {
          1:{text:'IC卡'},
          2:{text:'CPU卡'},
          3:{text:'身份证'},
          4:{text:'居住证'},
          5:{text:'银行卡'},
          6:{text:'APP'},
        },
        // filter: {
        //   placeholder: '请选择门卡类型',
        //   type: 'select',
        //   items: [
        //     {label: 'IC卡', value: 1},
        //     {label: 'CPU卡', value: 2},
        //     {label: '身份证', value: 3},
        //     {label: '居住证', value: 4},
        //     {label: '银行卡', value: 5},
        //     {label: 'APP', value: 6},
        //   ]
        // }
      },
      {
        index:'cardStatus',
        title:'门卡状态',
        type: ColumnType.tag,
        tag:{
          0:{text:'未激活',color: 'red'},
          1:{text:'已激活', color: 'green'},
          2:{text:'丢失', color: 'red'},
          3:{text:'损坏', color: 'red'},
          4:{text:'冻结', color: 'red'},
          5:{text:'待缴费', color: 'red'},
          6:{text:'已过期', color: 'red'},
          7:{text:'退卡', color: 'red'},
          9:{text:'其他'}
        },
        filter: {
          placeholder: '请选择门卡状态',
          type: 'select',
          items:[
            {label:'未激活',value: 0},
            {label:'已激活', value: 1},
            {label:'丢失', value: 2},
            {label:'损坏', value: 3},
            {label:'冻结', value: 4},
            {label:'待缴费', value: 5},
            {label:'已过期', value: 6},
            {label:'退卡', value: 7},
            {label:'其他', value: 9}
          ]
        }
      },
      {
        index: 'cardUsingStartTime',
        title: '最初使用时间'
      },
      {
        index: 'cardUsingEndTime',
        title: '最后使用时间'
      },
      {
        index: '',
        title: '操作',
        width:150,
        fixed:'right',
        buttons:[
          {
            text:'编辑',
            click:(_record)=>{
              this.editDic(_record);
            }
          },
        ]
      },
    ],
    showCheckBox: true,
    reqUrl:'/core/door/card/page',
    defaultParam:{"cardCategory":"3"}
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  
    //删除卡号后面的8个0
    delCardZero(num:string):string{
      if(num.length > 8){
        num = num.substring(0,num.length - 8);
      }
      return num;
    }

  ngOnInit() {

  }

  // 编辑功能开始
  editDic( _param?: object): void {
    const modal = this.modal.create({
      nzTitle: '修改楼栋权限',
      nzContent: ManageCardEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  
  //新增门卡
  addDoorCard(): void {
    const modal = this.modal.create({
      nzTitle: '新增门卡',
      nzContent: ManageCardAddComponent,
      nzWidth: 800,
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }

  _console(e: any): void {
    this.selectData = e;
  }
}
